package com.wgg.common.codec;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import com.caucho.hessian.io.SerializerFactory;

/**
 * @author WSH
 */
public class HessianSerializer implements Serializer {
	
	public static HessianSerializer INSTANCE = new HessianSerializer();
	
	private HessianSerializer () {}

	private final SerializerFactory DEFAULT_SERIALIZER_FACTORY = new SerializerFactory() {
        @Override
        public ClassLoader getClassLoader() {
            return Thread.currentThread().getContextClassLoader();
        }
    };
    
    @Override
    public Object deserialize(InputStream in) throws IOException {
    	Hessian2Input input = new Hessian2Input(in);
    	input.setSerializerFactory(DEFAULT_SERIALIZER_FACTORY);
    	return input.readObject();
    }
    
    @Override
    public void serialize(OutputStream os, Object object) throws IOException {
    	Hessian2Output output = new Hessian2Output(os);
    	output.setSerializerFactory(DEFAULT_SERIALIZER_FACTORY);
    	output.writeObject(object);
    	output.flush();
    }
}
